package com.maxwell.impl;

import com.maxwell.entity.Order;
import com.maxwell.inter.OrderManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Component;

import javax.mail.Message;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * User:Maxwell
 * Date:2015/2/16
 * Time:14:04
 */
@Component
public class SimpleJavaMailSender implements OrderManager {

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${mail.user.name}")
    private String fromMail;


    @Override
    public void placeOrder(final Order order) {
        MimeMessagePreparator preparator = new MimeMessagePreparator() {
            @Override
            public void prepare(MimeMessage mimeMessage) throws Exception {
                mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(order.getUserEmail(),"Maxwell's 126"));
                mimeMessage.setFrom(new InternetAddress(fromMail, "Maxwell's QQ"));
                mimeMessage.setText("Dear:" + order.getUserName() + ",This is A JavaMailSender Example!!");
                mimeMessage.setSubject(order.getSubject());
            }
        };
        javaMailSender.send(preparator);
    }
}
